package casa.depesos.app.com.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import casa.depesos.app.com.databinding.BannersItemBinding;
import casa.depesos.app.com.databinding.ProductItemBinding;
import casa.depesos.app.com.http.data.Banner;
import casa.depesos.app.com.it.BannerClickListener;

public class NoAdapter extends RecyclerView.Adapter {

    List<Banner> banner;
    List<Banner> product;
    int height;
    BannerClickListener activity;

    public NoAdapter(BannerClickListener activity, List<Banner> banner, List<Banner> product) {
        this(activity, banner, product, 0);
    }

    public NoAdapter(BannerClickListener activity, List<Banner> banner, List<Banner> product, int h) {
        this.banner = banner;
        this.product = product;
        this.height = h;
        this.activity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            BannersItemBinding binding = BannersItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new BannerHolder(binding);
        } else {
            ProductItemBinding binding = ProductItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ProductHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BannerHolder) {
            ((BannerHolder) holder).bind();
        } else if (holder instanceof ProductHolder) {
            ((ProductHolder) holder).bind(position);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (!banner.isEmpty() && position == product.size()) {
            return 1;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        if (banner.isEmpty()) {
            return product.size();
        }
        return product.size() + 1;
    }

    class ProductHolder extends RecyclerView.ViewHolder {
        ProductItemBinding binding;

        ProductHolder(ProductItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            Banner item = product.get(p);
            binding.title.setText(item.name);
            binding.tv.setText(item.title);
            Glide.with(binding.img).load(item.img).into(binding.img);
            itemView.setOnClickListener(view -> activity.onBannerClick(item));
        }
    }

    class BannerHolder extends RecyclerView.ViewHolder {
        BannersItemBinding binding;

        BannerHolder(BannersItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.getRoot().setLifecycleRegistry(activity.getLifecycle())
                    .setAdapter(new BannerAdapter(activity))
                    .create(banner);
        }

        void bind() {
            binding.getRoot().getLayoutParams().height = height;
        }
    }
}
